package com.dynamic.mybatis.core.generator.method;

import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.dynamic.mybatis.core.enums.ReturnType;
import com.dynamic.mybatis.core.generator.InjectorHelper;
import com.dynamic.mybatis.core.generator.InjectorMethod;
import com.dynamic.mybatis.core.generator.MapperMethod;
import com.dynamic.mybatis.core.generator.helper.InsertInjectorHelper;
import com.dynamic.mybatis.core.metadata.DynamicMappedStatement;
import com.dynamic.mybatis.core.metadata.MappedStatementField;
import com.dynamic.mybatis.core.metadata.MappedStatementMetaField;
import com.dynamic.mybatis.core.metadata.MappedStatementTable;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.Configuration;

import java.util.Map;
import java.util.Optional;

/**
 * @Description
 * @Author xs
 * @Date 2023/4/14 10:11
 */
public class Insert extends InjectorMethod {
    public Insert() {
        super(MapperMethod.StandardMapperMethod.insert);
    }

    public Insert(MapperMethod mapperMethod) {
        super(mapperMethod);
    }

    @Override
    public  String builderScriptSql(String namespace, MappedStatementTable table, MapperBuilderAssistant assistant){


        String columnScript = SqlScriptUtils.convertTrim(InsertInjectorHelper.getAllInsertSqlColumnMaybeIf(table,null),
                LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);
        String valuesScript = SqlScriptUtils.convertTrim(InsertInjectorHelper.getAllInsertSqlPropertyMaybeIf(table,null),
                LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);

        String sql = String.format(getSqlTemplate(), table.getTableName(),NEWLINE + SPACE + columnScript + NEWLINE, NEWLINE  + SPACE + valuesScript);

        return sql;
    }




    @Override
    public DynamicMappedStatement injectMappedStatement(String namespace, MappedStatementTable table, MapperBuilderAssistant assistant) {

        String sql = builderScriptSql(namespace,table,assistant);

        LanguageDriver languageDriver = assistant.getConfiguration().getDefaultScriptingLanguageInstance();
        Configuration configuration = assistant.getConfiguration();
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);

        Optional<MappedStatementMetaField> optionalField = InjectorHelper.getFirstKeyField(table);
        String keyProperty = null;
        String keyColumn = null;
        KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE;
        // 表包含主键处理逻辑,如果不包含主键当普通字段处理
        if (optionalField.isPresent()) {
            MappedStatementField field  = optionalField.get();
            if (field.isAutoincrement()) {
                /* 自增主键 */
                keyGenerator = Jdbc3KeyGenerator.INSTANCE;
                keyProperty = field.getField();
                // 去除转义符
                keyColumn = SqlInjectionUtils.removeEscapeCharacter(field.getColumn());
            }
        }
        MappedStatement ms =  super.addMappedStatement(assistant,table,namespace,sqlSource,SqlCommandType.INSERT,Map.class,null,Integer.class,keyGenerator,keyProperty,keyColumn);

        return builderDynamicMappedStatement(namespace,table,ms,sql);
    }
}